//
//  UserAccountViewModel.swift
//  Weibo
//
//  Created by apple on 15/12/18.
//  Copyright © 2015年 Leon. All rights reserved.
//

import UIKit
import AFNetworking

//封装网络请求 和 业务逻辑
class UserAccountViewModel: NSObject {
    
    var userAccount: UserAccount?
    
    override init() {
        //对象的构造分两类  先构造本类  再构造父类
        userAccount = UserAccount.loadAccount()
        super.init()
    }
    //计算性属性 用户是否登陆
    var userLogin: Bool {
        
        return userAccount?.access_token != nil
    }
    
    //用户头像的url
    var headURL: NSURL? {
        let url = NSURL(string: userAccount?.avatar_large ?? "")
        return url
    }
    
    //用户名
    var userName: String? {
        return userAccount?.name
    }
    
    var token: String? {
        return userAccount?.access_token
    }
    
    //加载用户的token 请求标识
    func loadAccessToken(code:String,finished:(isSuccess:Bool) -> ()) {
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":redirect_uri]
        
        let AFN = AFHTTPSessionManager()
        
        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        AFN.POST(urlString, parameters: parameters, progress: { (p) -> Void in
            print(p)
            }, success: { (_, result) -> Void in
                //print(result)
                if let dict = result as? [String : AnyObject] {
                    
                    //字典转模型
                    let userAccount = UserAccount(dict: dict)
                    //获取用户信息
                    self.loadUserInfo(userAccount,finished: finished)
                    
                }
            }) { (_, error) -> Void in
                print(error)
                finished(isSuccess: false)
        }
    }
    
    //获取用户信息
    private func loadUserInfo(account:UserAccount,finished:(isSuccess:Bool) -> ()) {
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        //字典中不能存放nill
        if let token = account.access_token,uid = account.uid {
            let parameters = ["access_token": token, "uid": uid]
            
            let AFN = AFHTTPSessionManager()
            
            AFN.GET(urlString, parameters: parameters, progress: { (_) -> Void in
                }, success: { (_, result) -> Void in
                    print(result)
                    if let dict = result {
                        //获取用户信息
                        let avatar_large = dict["avatar_large"] as! String
                        let name = dict["name"] as! String
                        //给对应属性赋值
                        account.name = name
                        account.avatar_large = avatar_large
                        //获取用户信息的完整自定义对象
                        //存储自定义对象
                        account.saveAccount()
                        //执行成功的回调
                        finished(isSuccess: true)
                        
                        //print(avatar_large,name)
                    }
                }) { (_, error) -> Void in
                    
                    finished(isSuccess: false)
                    print(error)
            }
            
        }
    }
    
}
